var server = require('../../../utils/server');
import Toast from '../../../miniprogram_npm/vant-weapp/toast/toast';
Page({
    add: function() {
        wx.navigateTo({
            url: '../add/add'
        });
    },
    onShow: function() {
        this.loadData();
    },
    setDefault: function(e) {
        

        let _this  =this;

        let _params={
            address_id:e.currentTarget.dataset.id,
            username:getApp().globalData.username,
            token:getApp().globalData.token
        }
        // server.postJSON('/person/cahngdefault',_params,res=>{
        //     if(res.data.status==200){
        //         Toast('修改成功');
        //         _this.getaddrlist();
        //     }   
        // })
      server.send('/person/cahngdefault','POST',_params).then(res=>{
        if (res.status == 200) {
                Toast('修改成功');
                _this.getaddrlist();
            }  
      })

    },
    edit: function(e) {
        var that = this;
        var objectId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../edit/index?address_id=' + objectId
        });
    },
    delete: function(e) {
        var that = this;
        // 取得下标
        // 找到当前地址AVObject对象
        var address_id = e.currentTarget.dataset.id;
        // 给出确认提示框
        wx.showModal({
            title: '确认',
            content: '要删除这个地址吗？',
            success: function(res) {
                if (res.confirm) {
                    let _params={
                        address_id: address_id,
                        username:getApp().globalData.username,
                        name: getApp().globalData.name,
                    }
                    // server.postJSON('/person/deleteaddress' , _params,function(res) {
                    //     wx.showToast({
                    //         title: res.data.msg,
                    //         icon: 'success',
                    //         duration: 8000
                    //     });
                    //     that.getaddrlist();
                    // })
                  server.send('/person/deleteaddress','POST',_params).then(res=>{
                    wx.showToast({
                            title: res.msg,
                            icon: 'success',
                            duration: 8000
                        });
                        that.getaddrlist();
                  })



                }
            }
        })

    },
    loadData: function() {
        this.getaddrlist();
    },
    getaddrlist(){
        var that = this;
        // server.postJSON('/person/address', {
        //     username: getApp().globalData.username,
        //     token: getApp().globalData.token,
        // }, function (res) {
        //     if (res.data.status == 200) {
        //         console.log(res.data)
        //         that.setData({
        //             addressObjects: res.data.list
        //         });
        //     }
        // });
      server.send('/person/address','POST',{
            username: getApp().globalData.username,
            token: getApp().globalData.token
      }).then(res=>{
        if (res.status == 200) {
                that.setData({
                    addressObjects: res.list
                });
            }
      })
    }
})